//
//  HomeImgScrollCell.m
//  ReceiptDelivery
//
//  Created by 吕晶 on 2021/6/22.
//  Copyright © 2021 吕晶. All rights reserved.
//

#import "HomeImgScrollCell.h"
#import <SDCycleScrollView/SDCycleScrollView.h>

@interface HomeImgScrollCell () <SDCycleScrollViewDelegate>

@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) SDCycleScrollView *scrollView;
@property (nonatomic, strong) UIPageControl *pageControl;

@end

@implementation HomeImgScrollCell

+ (instancetype)HomeImgScrollCellWithTableView:(UITableView *)tableView {
    static NSString *cellID = @"HomeImgScrollCell";
    HomeImgScrollCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[HomeImgScrollCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = [UIColor clearColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        self.bgView = [[UIView alloc] initWithFrame:CGRectMake(10, 0, ScreenW - 20, ScreenW * (95. / 355.))];
        self.bgView.backgroundColor = HexColor(0xffffff);
        self.bgView.layer.masksToBounds = YES;
        self.bgView.layer.cornerRadius = 6;
        [self.contentView addSubview:self.bgView];
        
        self.scrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, ScreenW - 20, ScreenW * (95. / 355.)) delegate:self placeholderImage:[UIImage imageNamed:@""]];
        self.scrollView.backgroundColor = HexColor(0xffffff);
        self.scrollView.layer.masksToBounds = YES;
        self.scrollView.layer.cornerRadius = 6;
//        self.scrollView.pageControlStyle = SDCycleScrollViewPageContolStyleClassic;
//        self.scrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
//        self.scrollView.pageControlDotSize = CGSizeMake(10, 10);
//        self.scrollView.currentPageDotColor = Color_ThemeColor;
//        self.scrollView.pageDotColor = HexColor(0xC3C4CC);
//        self.scrollView.currentPageDotImage = [UIImage imageNamed:@"home_pagecontrol_selected_img"];
//        self.scrollView.pageDotImage = [UIImage imageNamed:@"home_pagecontrol_no_selected_img"];
        self.scrollView.autoScrollTimeInterval = 5.0;
        self.scrollView.showPageControl = NO;
        
//        self.scrollView.bounces = YES;
//        self.scrollView.showsHorizontalScrollIndicator = NO;
//        self.scrollView.pagingEnabled = YES;
//        self.scrollView.delegate = self;
        [self.bgView addSubview:self.scrollView];
        
        self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, self.scrollView.nim_bottom - 10, ScreenW - 20, 5)];
        self.pageControl.pageIndicatorTintColor = HexColor(0xC3C4CC);
        self.pageControl.currentPageIndicatorTintColor = Color_ThemeColor;
//        if (@available(iOS 14.0, *)) {
//            self.pageControl.preferredIndicatorImage = [UIImage imageNamed:@"home_pagecontrol_selected_img"];
//        } else {
//            // Fallback on earlier versions
//        }
        [self.bgView addSubview:self.pageControl];
        
        self.height = self.scrollView.nim_bottom;
    }
    return self;
}

-(void)setImgArray:(NSArray<NSString *> *)imgArray {
    _imgArray = imgArray;
    [self configUI];
}

- (void)configUI {
    self.scrollView.imageURLStringsGroup = self.imgArray;
//    self.scrollView.contentSize = CGSizeMake(self.scrollView.nim_width * self.imgArray.count, self.scrollView.nim_height);
//    [self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
//    [self.imgArray enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(self.scrollView.nim_width * idx, 0, self.scrollView.nim_width, self.scrollView.nim_height)];
//        imgView.layer.masksToBounds = YES;
//        imgView.layer.cornerRadius = 6;
//        imgView.backgroundColor = [UIColor lightGrayColor];
//        [imgView sd_setImageWithURL:[NSURL URLWithString:obj]];
//        [self.scrollView addSubview:imgView];
//    }];
    self.pageControl.numberOfPages = self.imgArray.count;
    if (self.imgArray.count > 1) {
        self.pageControl.hidden = NO;
    }else {
        self.pageControl.hidden = YES;
    }
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
//    self.pageControl.currentPage = self.scrollView.contentOffset.x / self.scrollView.nim_width;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
